package com.zmh.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.zmh.common.DateUtil;
import com.zmh.common.OperationUtil;
import com.zmh.model.wechat.httpClient.Wimage;

/**
 * wimage 数据操作
 * @author zhoumh
 * @version  创建时间：2017-12-28 19:31:28
 * @Description  
 */ 

@Repository
public class WimageDao{

	private static final Logger LOGGER = LoggerFactory.getLogger(WimageDao.class);
	@Autowired 
	private JdbcTemplate jdbcTemplate; 
	/**
	 * 查询全部信息
	 * @return List<Wimage>
	 * @author zhoumh
	 * @version  创建时间：2017-12-28 19:31:28
	 * @Description  
	 */ 
	public List<Wimage> selWimageByAll(){
		String sqlstr = "SELECT id, uuid, wid, wurl, name, sendtime, size, status, type, state, createtime, updatetime, description FROM wimage";
		List<Wimage> wimages = new ArrayList<>();
		try { 
			wimages = jdbcTemplate.query(sqlstr, new RowMapper<Wimage>(){
				@Override
				public Wimage mapRow(ResultSet rs, int rowNum) throws SQLException {
					return objSet(rs, rowNum); 
				} 
				}); 
		} catch (EmptyResultDataAccessException e) { 
			e.printStackTrace(); 
		} 
		return wimages; 
	}
	/**
	 * 根据UUID查询信息
	 * @param uuid
	 * @return Wimage
	 * @author zhoumh
	 * @version  创建时间：2017-12-28 19:31:28
	 * @Description  
	 */ 
	public Wimage selWimageByAll(String uuid){
		String sqlstr = "SELECT id, uuid, wid, wurl, name, sendtime, size, status, type, state, createtime, updatetime, description FROM wimage WHERE uuid=?";
		Wimage wimage = new Wimage();
		Object[] param = new Object[]{uuid};
		try { 
			wimage = jdbcTemplate.queryForObject(sqlstr, param, new RowMapper<Wimage>(){
				@Override
				public Wimage mapRow(ResultSet rs, int rowNum) throws SQLException {
					return objSet(rs, rowNum); 
				} 
				}); 
		} catch (EmptyResultDataAccessException e) { 
			e.printStackTrace(); 
		} 
		return wimage; 
	}
	/**
	 * 根据发送时间查询最远的一条
	 * @param uuid
	 * @return Wimage
	 * @author zhoumh
	 * @version  创建时间：2017-12-28 19:31:28
	 * @Description  
	 */ 
	public Wimage selWimageByOne(){
		String sqlstr = "SELECT id, uuid, wid, wurl, name, sendtime, size, status, type, state, createtime, updatetime, description FROM wimage ORDER BY SENDTIME LIMIT 1";
		Wimage wimage = new Wimage();
		try { 
			wimage = jdbcTemplate.queryForObject(sqlstr, new RowMapper<Wimage>(){
				@Override
				public Wimage mapRow(ResultSet rs, int rowNum) throws SQLException {
					return objSet(rs, rowNum); 
				} 
				}); 
		} catch (EmptyResultDataAccessException e) { 
			e.printStackTrace(); 
		} 
		return wimage; 
	}
	/**
	 * 根据状态查询总条数
	 * @param state
	 * @return int
	 * @author zhoumh
	 * @version  创建时间：2017-12-28 19:31:28
	 * @Description  
	 */ 
	public int sumByState(String state){
		String sqlstr = "  SELECT count(id) FROM wimage WHERE state = ?";
		Object[] param = new Object[]{state};
		int count = jdbcTemplate.queryForObject(sqlstr,param,Integer.class);
		return count;
	}
	/**
	 * 插入数据
	 * @param state
	 * @return int
	 * @author zhoumh
	 * @version  创建时间：2017-12-28 19:31:28
	 * @Description  
	 */ 
	public int insWimage(Wimage wimage){
		String sqlstr = "insert into wimage ( uuid, wid, wurl, name, sendtime, size, status, type, state, description) values(?,?,?,?,?,?,?,?,?,?)";
		Object[] param = new Object[]{OperationUtil.getUUID(),wimage.getWid(),wimage.getWurl(),wimage.getName(),wimage.getSendtime(),wimage.getSize(),wimage.getStatus(),wimage.getType(),wimage.getState(),wimage.getDescription()};
		int result = jdbcTemplate.update(sqlstr, param);
		return result;
	}
	/**
	 * 根据UUID修改信息
	 * @param uuid
	 * @return Wimage
	 * @author zhoumh
	 * @version  创建时间：2017-12-28 19:31:28
	 * @Description  
	 */ 
	public int updWimageByUuid(Wimage wimage){
		String sqlstr = "update wimage set wid=?, wurl=?, name=?, sendtime=?, size=?, status=?, type=?, state=?, updatetime=?, description=? WHERE uuid=?";
		Object[] param = new Object[]{wimage.getWid(),wimage.getWurl(),wimage.getName(),wimage.getSendtime(),wimage.getSize(),wimage.getStatus(),wimage.getType(),wimage.getState(),DateUtil.getDateTime(),wimage.getDescription(), wimage.getUuid()};
		int result = jdbcTemplate.update(sqlstr, param);
		return result; 
	}
	/**
	 * 根据uuid删除书籍
	 * @param uuid
	 * @return int
	 * @author zhoumh
	 * @version  创建时间：2017-12-28 19:31:28
	 * @Description  
	 */ 
	public int delWimageByUuid(String uuid){
		String sqlstr = "  delete from wimage where uuid = ? ";
		Object[] param = new Object[]{uuid};
		int result = jdbcTemplate.queryForObject(sqlstr,param,Integer.class);
		return result;
	}
	/**
	 * 结果集映射
	 * @author zhoumh
	 * @version  创建时间：2017-12-28 19:31:28
	 * @Description  
	 */ 
	private Wimage objSet(ResultSet rs, int rowNum) throws SQLException{
		Wimage wimage = new Wimage();
		wimage.setId(rs.getLong("id"));
		wimage.setUuid(rs.getString("uuid"));
		wimage.setWid(rs.getString("wid"));
		wimage.setWurl(rs.getString("wurl"));
		wimage.setName(rs.getString("name"));
		wimage.setSendtime(rs.getString("sendtime"));
		wimage.setSize(rs.getString("size"));
		wimage.setStatus(rs.getString("status"));
		wimage.setType(rs.getString("type"));
		wimage.setState(rs.getString("state"));
		wimage.setCreatetime(rs.getString("createtime"));
		wimage.setUpdatetime(rs.getString("updatetime"));
		wimage.setDescription(rs.getString("description"));
		return wimage;
	};
}

